package com.alex.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class CommonUtil {


    /**
     * 获取唯一的uuid字符串
     * @return
     */
    public static String getUUID(){
        String randomStr = UUID.randomUUID().toString().replace("-", "");
        return randomStr;
    }

    /**
     * 获取根据时间和随机数生成主键
     * @return
     */
    public static String getTimeRandomId(){
        Date now = new Date();
        StringBuilder str = new StringBuilder(dateToStr(now, "yyMMddhhmmss"));

        int t = new Random().nextInt(999999); //生成0~999999的随机整数，但不包括999999
        String id = String.format("%06d", t + 1);
        str.append(id);

        return str.toString();
    }

    /**
     * 时间转字符串
     * @param date  时间
     * @param formatStr 字符串格式
     * @return
     */
    public static String dateToStr(Date date,String formatStr){
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date);
    }
}
